package com.lxx.train.member.controller;

import com.lxx.train.common.resp.CommonResp;
import com.lxx.train.member.req.MemberLoginReq;
import com.lxx.train.member.req.MemberSendCodeReq;
import com.lxx.train.member.resp.MemberLoginResp;
import com.lxx.train.member.service.MemberService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @GetMapping("/hello")
    public CommonResp<String> hello() {
        return CommonResp.ok("hello!");
    }


    /**
     * 发送验证码
     * 如果手机号不存在，则插入一条记录（相当于注册）
     *
     * @param req 手机号
     * @return
     */
    @PostMapping("/send-code")
    public CommonResp sendCode(@Valid @RequestBody MemberSendCodeReq req) {
        memberService.sendCode(req);
        return CommonResp.ok();
    }

    /**
     * 登录
     *
     * @param req 手机号 验证码
     * @return
     */
    @PostMapping("/login")
    public CommonResp<MemberLoginResp> login(@Valid @RequestBody MemberLoginReq req) {
        MemberLoginResp resp = memberService.login(req);
        return CommonResp.ok(resp);
    }


}
